package com.sk.weichat.ui.me.wallet;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jakewharton.rxbinding2.widget.TextViewAfterTextChangeEvent;
import com.sk.weichat.R;
import com.sk.weichat.ui.base.EditBaseActivity;
import com.sk.weichat.util.ToastUtil;
import com.sk.weichat.view.ClearEditText;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import okhttp3.Call;

public class VerifyRealNameInfoActivity extends EditBaseActivity implements View.OnClickListener {
    private Button btnConfirm;
    private ImageView ivBack;
    private ClearEditText edtInputName;
    private ClearEditText edtInputIdcard;
    private final static int request_set_pay_pswd = 3218;
    private final static int request_set_pay_question = 3849;//设置安全问题

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_real_name);
        ivBack = findViewById(R.id.iv_title_left);
        TextView tv_title_center = findViewById(R.id.tv_title_center);
        tv_title_center.setText(getString(R.string.idcar_infor));
        edtInputName = findViewById(R.id.edit_input_name);
        edtInputIdcard = findViewById(R.id.edit_input_idcard);
        ivBack.setOnClickListener(this);
        btnConfirm = findViewById(R.id.btn_sure);
        btnConfirm.setOnClickListener(this);
        //弹出键盘输入
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

        RxTextView.afterTextChangeEvents(edtInputIdcard).mergeWith(RxTextView.afterTextChangeEvents(edtInputName))
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<TextViewAfterTextChangeEvent>() {
                    @Override
                    public void accept(TextViewAfterTextChangeEvent textViewAfterTextChangeEvent) throws Exception {
                        if (!TextUtils.isEmpty(edtInputName.getText())) {
                            btnConfirm.setEnabled(true);
                            btnConfirm.setBackgroundResource(R.drawable.shape_corner_26dp_solid_primary);
                        } else {
                            btnConfirm.setEnabled(false);
                            btnConfirm.setBackgroundResource(R.drawable.shape_corner_26_solid_gray_bdc2c8);
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
        edtInputName.setFocusable(true);
        edtInputName.setFocusableInTouchMode(true);
        edtInputName.requestFocus();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sure:
                //验证释明心
                verifyInfomation(edtInputName.getText().toString(), edtInputIdcard.getText().toString(), coreManager.getSelfStatus().accessToken);
                break;
            case R.id.iv_title_left:
                finish();
                break;
        }
    }

    private void verifyInfomation(String name, String idCard, String access_token) {
        Map<String, String> params = new HashMap<>();
        params.put("access_token", access_token);
        params.put("name", name);
        params.put("idCard", idCard);
        HttpUtils.post().url(coreManager.getConfig().VERIFY_IDENTIFY_INFOMATION)
                .params(params)
                .build()
                .execute(new BaseCallback<Void>(Void.class) {
                    @Override
                    public void onResponse(ObjectResult<Void> result) {
                        if (result.getResultCode() == 1) {
                            //验证成功,重新设置安全问题
                            startActivityForResult(new Intent(mContext, SetIdentifyQuestionActivity.class), request_set_pay_question);

                        } else {
                            ToastUtil.showToast(mContext, result.getResultMsg());
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e) {

                    }
                });
    }

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void helloEventBus(String str) {
        if (str.equals("set_question")) {
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

       // startActivityForResult(new Intent(mContext, ResetPayPwdNewActivity.class), request_set_pay_pswd);
        if (requestCode == request_set_pay_pswd && resultCode == Activity.RESULT_OK) {
            setResult(Activity.RESULT_OK);
            finish();
        }
    }
}
